
import {request} from '../../request/index.js'
import regeneratorRuntime, { async } from '../../lib/runtime/runtime';
var QQMapWX = require("../../utils/qqmap-wx-jssdk.min");
var qqmapsdk;
import Notify from '../../vant/dist/notify/notify'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      // 总上报隐患
      updgeForm:[],
      // 已审核 yes，fail
      checkedForm: [],
      Ifchecked1: true,
      Ifchecked1: false,
      // 待审核 no
      waitForm:[],
      Ifwait:true,
      Ifwait1:false,
      // 已消除 done/undone
      finishForm: [],   
      Iffinish:true,
      Iffinish1:false,
      jiazai: false,

      // address: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'TUHBZ-ZPFKU-XQAVA-BC4M4-ER4IZ-BXBLL'
    });
   this.getupdgeForm(false)
  },

  ToSee: function(event){
    var lat = event.currentTarget.dataset.dgelat
    var lng = event.currentTarget.dataset.dgelng

    console.log(lat,lng)
    this.jingWeiduToDiZhi(lat,lng)
  },
   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // qqmapsdk = new QQMapWX({
    //   key: 'TUHBZ-ZPFKU-XQAVA-BC4M4-ER4IZ-BXBLL'
    // });
    this.getupdgeForm(true);
    console.log(1)
  },
  async getupdgeForm(refresh){
    if (wx.getStorageSync('role') === 'admin'){
      const res = await request({
        url: '/danger/findAll',
        header:{
          'Authorization':'Bearer '+wx.getStorageSync('token'),
        },
        method: "get",
      })
      console.log(res)
      if (res.data.status !== 200){
        var title = res.data.msg
        wx.showToast({
          title: title,
          icon: 'none',
          duration: 2000
        })
        if (title == '您还未登录'){
           wx.reLaunch({
            url: '/pages/index/index',
          })
        }
      }
      else {
        if (! refresh) {
          wx.showLoading({
           title: '拼命加载中',
         })
       }
        var updgeForm = res.data.data.reverse()
        if (updgeForm[0]) {
          var checked = [];
          var wait = [];
          var finish = [];
          updgeForm.forEach(function(item){
            if (item.dgeStatus == 'yes' || item.dgeStatus == 'fail'){
              var infos = (item.dgeInfo+ "").split('imgGroup:')[0]
              var info = (infos === 'null') ? '暂无隐患信息' : infos
              var imgstr = (item.dgeInfo+ "").split('imgGroup:')[1]
              var arr = []
              if(imgstr) {
                  arr = (imgstr+ "").split(",")
              }
              else {
                  arr = ["null"]
              }
             checked.push({item,info: info,img:arr})
            } else if (item.dgeStatus == 'no'){
              var infos = (item.dgeInfo+ "").split('imgGroup:')[0]
              var info = (infos === 'null') ? '暂无隐患信息' : infos
              var imgstr = (item.dgeInfo+ "").split('imgGroup:')[1]
              var arr = []
              if(imgstr) {
                  arr = (imgstr+ "").split(",")
              }
              else {
                  arr = ["null"]
              }
              wait.push({item,info: info,img:arr})
            }else{
              var infos = (item.dgeInfo+ "").split('imgGroup:')[0]
              var info = (infos === 'null') ? '暂无隐患信息' : infos
              var imgstr = (item.dgeInfo+ "").split('imgGroup:')[1]
              var arr = []
              if(imgstr) {
                  arr = (imgstr+ "").split(",")
              }
              else {
                  arr = ["null"]
              }
              finish.push({item,info: info,img:arr})
            }
           })
           console.log(wait)
           if(checked[0]) {
            this.setData({
              Ifchecked:false
            })
          }
          else {
            this.setData({
              Ifchecked1:true
            })
          }
          if(wait[0]) {
            this.setData({
              Ifwait:false
            })
          }
          else{
            this.setData({
              Ifwait1:true
            })
          }
          if(finish[0]) {
            this.setData({
              Iffinish:false
            })
          }
          else {
            this.setData({
              Iffinish1:true
            })
          }
          if (refresh) {
            this.setData({
              updgeForm: updgeForm,
              checkedForm: checked,
              waitForm: wait,
              finishForm: finish,
              jiazai: true
            },wx.stopPullDownRefresh())
          }
          else {
            this.setData({
              updgeForm: updgeForm,
              checkedForm: checked,
              waitForm: wait,
              finishForm: finish,
              jiazai: true
            },wx.hideLoading({
              success: (res) => {},
            }))
          }
          
        }
        else{
          // 没有上报隐患记录
          if (refresh) {
            this.setData({
              Ifchecked1: true,
              Ifwait1:true,
              Iffinish1:true
            },wx.stopPullDownRefresh({
              success: (res) => {},
            }))
          }
          else {
             this.setData({
              Ifchecked1: true,
              Ifwait1:true,
              Iffinish1:true
            }, wx.hideLoading({
              success: (res) => {},
            }))
          }
        }
      }
    }
    else{
      const res = await request({
        url: '/danger/findByDept/' + wx.getStorageSync('deptId'),
        header:{
          'Authorization':'Bearer '+wx.getStorageSync('token'),
        },
        method: "get",
      })
      console.log(res)
      if (res.data.status !== 200){
        var title = res.data.msg
        wx.showToast({
          title: title,
          icon: 'none',
          duration: 2000
        })
        if (title == '您还未登录'){
           wx.reLaunch({
            url: '/pages/index/index',
          })
        }
      }
      else {
        if (! refresh) {
          wx.showLoading({
           title: '拼命加载中',
         })
       }
        var updgeForm = res.data.data.reverse()
        if (updgeForm[0]) {
          var checked = [];
          var wait = [];
          var finish = [];
          updgeForm.forEach(function(item){
            if (item.dgeStatus == 'yes' || item.dgeStatus == 'fail'){
              var infos = (item.dgeInfo+ "").split('imgGroup:')[0]
              var info = (infos === 'null') ? '暂无隐患信息' : infos
              var imgstr = (item.dgeInfo+ "").split('imgGroup:')[1]
              var arr = []
              if(imgstr) {
                  arr = (imgstr+ "").split(",")
              }
              else {
                  arr = ["null"]
              }
             checked.push({item,info: info,img:arr})
            } else if (item.dgeStatus == 'no'){
              var infos = (item.dgeInfo+ "").split('imgGroup:')[0]
              var info = (infos === 'null') ? '暂无隐患信息' : infos
              var imgstr = (item.dgeInfo+ "").split('imgGroup:')[1]
              var arr = []
              if(imgstr) {
                  arr = (imgstr+ "").split(",")
              }
              else {
                  arr = ["null"]
              }
              wait.push({item,info: info,img:arr})
            }else{
              var infos = (item.dgeInfo+ "").split('imgGroup:')[0]
              var info = (infos === 'null') ? '暂无隐患信息' : infos
              var imgstr = (item.dgeInfo+ "").split('imgGroup:')[1]
              var arr = []
              if(imgstr) {
                  arr = (imgstr+ "").split(",")
              }
              else {
                  arr = ["null"]
              }
              finish.push({item,info: info,img:arr})
            }
           })
           console.log(wait)
           if(checked[0]) {
            this.setData({
              Ifchecked:false
            })
          }
          else {
            this.setData({
              Ifchecked1:true
            })
          }
          if(wait[0]) {
            this.setData({
              Ifwait:false
            })
          }
          else{
            this.setData({
              Ifwait1:true
            })
          }
          if(finish[0]) {
            this.setData({
              Iffinish:false
            })
          }
          else {
            this.setData({
              Iffinish1:true
            })
          }
          if (refresh) {
            this.setData({
              updgeForm: updgeForm,
              checkedForm: checked,
              waitForm: wait,
              finishForm: finish,
              jiazai: true
            },wx.stopPullDownRefresh())
          }
          else {
            this.setData({
              updgeForm: updgeForm,
              checkedForm: checked,
              waitForm: wait,
              finishForm: finish,
              jiazai: true
            },wx.hideLoading({
              success: (res) => {},
            }))
          }
          
        }
        else{
          // 没有上报隐患记录
          if (refresh) {
            this.setData({
              Ifchecked1: true,
              Ifwait1:true,
              Iffinish1:true
            },wx.stopPullDownRefresh({
              success: (res) => {},
            }))
          }
          else {
             this.setData({
              Ifchecked1: true,
              Ifwait1:true,
              Iffinish1:true
            }, wx.hideLoading({
              success: (res) => {},
            }))
          }
         
        }
      }
    }
   
  },


// 经纬度转换成地址
jingWeiduToDiZhi(latitude,longitude) {
  let that = this;
  // 调用接口
  qqmapsdk.reverseGeocoder({
    location: {
      latitude: latitude ? latitude : null,
      longitude: longitude ? longitude : null
    },
    coord_type: 3, //baidu经纬度
    success: function(res){
      // console.log(res);
      // let tempData = res.result.address_component;
      Notify({ type: 'primary', message: res.result.address });
      //   that.setData({
      //   address: res.result.address,
      //   // address: res.result.formatted_addresses.recommend,
      // },() => {
        
      // });
      
      
    },
    fail: function(error) {
      console.log("err")
      console.error(error);
    },
    complete: function(res) {
      // console.log(res);
    }
  })
},

})